// scaup - Scala up!
// (c) 2009, Normen Müller, normen.mueller@gmail.com  
// $Id: Parser.scala 89 2010-05-11 21:58:00Z normen.mueller $
package locutor.draft.dg.parsing

import java.io.File
import java.net.URL
import java.text.MessageFormat.{format}

import collection.mutable.HashSet

import scalaxx.path._
//import scalaxx.parsing.path.XPathParser

import scaup.Views._

//import locutor.util.parsing._
/** A dependency graph parser.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.5 */
//private [dg] class Parser extends BaseParser {
//  /* Fill in abstract defs*/
//  type Tokens = Lexer
//  val lexical: Tokens = new Lexer
//
//  /* Configure lexical parsing */                                 
//  lexical.reserved ++= new HashSet[String] ++ List("digraph", "for", "at", "using", "set") ++ BaseParser.reserved
//  
//  lexical.delimiters ++= new HashSet[String] ++ List("{", "}", "(", ")", "-:", ":", "=", ";", "->", "<", ">") ++ BaseParser.delimiters
//
//  /* Define the grammar */  
//  def dg(description: File) = graph(description) >> constituents
//  
//  def graph(description: File) = (("digraph" ~> text) ~ ("for" ~>  string) ~ ("at" ~> numeric))  ~ ("using" ~> model) ^^ {
//    case name ~ ref ~ rev ~ model =>
//      new DependencyGraph(name, ref.toFile.resolveWrt(description).toFile, rev.toInt, model)      
//  }
//  
//  lazy val model = string ^^ {
//    case model => ((m: File) =>
//      parse((m.pf / (m.name.toLowerCase + ".gm")).content)
//    )((model replaceFirst("\\$LOCUTOR_MODELS", System.getProperty("locutor.models"))).toFile)
//  }
//  
//  lazy val constituents = (g: DependencyGraph) => ("{" ~> (nodes(g) >> edges) <~ "}")
//  
//  lazy val nodes = (g: DependencyGraph) => (((text ~ (":" ~> ident) ~ attributes))*) ^^ {
//    case nodes =>
//      for(node <- nodes) node match {
//        case id ~ ty ~ as =>
//          val url = (as get "url") match {
//            case Some(u) => u.toURL
//            case None    => g.reference.path.toFile.toURL
//          }
//          val xpath = (as get "xpath") match {
//            //@todo eliminate asInstanceOf
//            case Some(x) => XPathParser.parse(x).asInstanceOf[LocationPath]
//            case None    => error("Fatal DG parsing error: '" + id + "' is missing XPath attribute")
//          }
//          g + (id, url, xpath, (Map("type" -> ty) ++ (as - "url" - "xpath")))
//      }
//      g
//  }
//  
//  lazy val edges = (g: DependencyGraph) => ((text ~ ("-:" ~> ident) ~ opt(attributes) ~ ("->" ~> text))*) ^^ {
//    case edges => 
//      for(edge <- edges) edge match {
//        case src ~ ty ~ as ~ tgt => (g \ src, g \ tgt) match {
//          case (Some(s), Some(t)) => (s connectTo t)(Map("type" -> ty) ++ as.getOrElse(Nil))
//          case _                  => error("Fatal DG parsing error: Some edge declaration is not syntax conform.")
//        }
//      }
//      g
//  }
//  
//  lazy val attributes = "(" ~> ((attribute <~ opt(","))*) <~ ")" ^^ { case as => Map() ++ as }
//                                                                
//  lazy val attribute = (ident <~ "=") ~ (set | text | numeric) ^^ { case k ~ v => (k, v) }
//  
//  lazy val set =
//    ("set" ~ "<") ~> (primitives <~ ">") ~ ("{" ~> (((string ^^ {case s => s mkString ("\"", "", "\"")} | numeric) <~ opt(","))*) <~ "}") ^^ {
//      case ty ~ vals => "set<"+ty+">"+"{"+  (vals mkString ",") +"}"
//    }
//
//}
